﻿Imports System.WeakReference
Imports System.Web.SessionState
Imports System.Security
Imports System.Security.Principal.WindowsIdentity
Imports System.Web
Imports System.Web.UI
Imports System.Web.HttpContext
Imports System.Security.Principal
Imports sonicERPDAL
Imports sonicERPinfo
Imports System.Globalization
Imports System.Reflection



Partial Public Class masterprincipal
    Inherits MasterBase




#Region "Eventos do Form"

    Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        'Dim principal As IPrincipal
        'principal = HttpContext.Current.User
        'If Not principal.Identity.IsAuthenticated Then
        '    Response.Redirect("~/login.aspx?id=1")
        'End If
    End Sub


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try


            If Not IsPostBack Then
                hora()
                lblusuario.Text = sessaousuario.usuario.apelido.ToUpper()
                getmemu()
            Else

            End If

        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Sub


    

    'Public Sub getmemu()
    '    Dim dal As New menuDAL
    '    Dim entidade As New Menu
    '    Menu1.Items.Clear()
    '    entidade.idnivel = idnivel
    '    Dim dtmenu As DataTable = dal.getmenu1(entidade)
    '    Dim dtsubmenu As DataTable = dal.menumasterpage(entidade)

    '    For Each menu As DataRow In dtmenu.Rows
    '        Dim pai As New MenuItem(menu.Item(1).ToString(), menu.Item(0).ToString())
    '        For Each submenu As DataRow In dtsubmenu.Rows
    '            If menu.Item(0).ToString() = submenu.Item(2).ToString() Then

    '                Dim filho As New MenuItem(submenu.Item(1).ToString(), submenu.Item(0).ToString() & "submenu" & submenu.Item(2).ToString(), "", submenu.Item(3).ToString(), "")
    '                pai.ChildItems.Add(filho)
    '            End If
    '        Next
    '        If pai.ChildItems.Count > 0 Then
    '            Menu1.Items.Add(pai)
    '        End If
    '    Next
    'End Sub

    Public Sub getmemu()
        ' variavel i contar os filhos
        Dim i As Integer = 0
        Dim dal As New menuDAL
        sessaousuario.menu.idnivel = sessaousuario.usuario.idnivel
        Dim dtmenu As DataTable = dal.getmenu1(sessaousuario.menu)
        Dim dtsubmenu As DataTable = dal.menumasterpage(sessaousuario.menu)
        For Each menu As DataRow In dtmenu.Rows
            i = 0
            For Each submenu As DataRow In dtsubmenu.Rows
                'adicionar filhos
                If menu.Item(0).ToString() = submenu.Item(2).ToString() Then
                    If i = 0 Then
                        SlideMenu1.AddParent(menu.Item(0).ToString(), menu.Item(1).ToString())
                    End If
                    SlideMenu1.AddChild(submenu.Item(1).ToString(), submenu.Item(1).ToString(), submenu.Item(3).ToString(), "", "", "iframe")
                    i += 1

                End If
            Next

        Next
    End Sub


    'Public Sub getmenumaster()
    '    Dim dal As New menuDAL
    '    Dim entidade As New Menu

    '    entidade.idnivel = idnivel
    '    Trvmenu.Nodes.Clear()
    '    Trvmenu.CheckedNodes.Clear()
    '    Dim dtmenu As DataTable = dal.getmenu1(entidade)
    '    Dim dtsubmenu As DataTable = dal.menumasterpage(entidade)

    '    For Each menu As DataRow In dtmenu.Rows
    '        Dim trmenu As New TreeNode(menu.Item(1).ToString(), menu.Item(0).ToString(), "", "javascript:void(0)", "")
    '        trmenu.SelectAction = TreeNodeSelectAction.Expand

    '        For Each submenu As DataRow In dtsubmenu.Rows
    '            If menu.Item(0).ToString() = submenu.Item(2).ToString() Then
    '                Dim trsubmenu As New TreeNode(submenu.Item(1).ToString(), submenu.Item(0).ToString() & "submenu" & submenu.Item(2).ToString(), "", submenu.Item(3).ToString(), "")

    '                trmenu.ChildNodes.Add(trsubmenu)

    '            End If
    '        Next
    '        Trvmenu.Nodes.Add(trmenu)

    '    Next


    'End Sub
    Public Sub hora()



        Select Case DateTime.Now.Hour

            Case 0 To 12

                lblhora.Text = "Bom Dia"
            Case 12 To 18
                lblhora.Text = "Boa Tarde"
            Case Else
                lblhora.Text = "Boa Noite"
        End Select

    End Sub



    Protected Sub imgsair_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgsair.Click
        FormsAuthentication.SignOut()
        HttpContext.Current.Session.Clear()
        HttpContext.Current.Session.Abandon()
        Dim cookie1 As HttpCookie = New HttpCookie(FormsAuthentication.FormsCookieName, "")
        Response.Cookies.Add(cookie1)
        Dim cookie2 As HttpCookie = New HttpCookie("ASP.NET_SessionId", "")
        cookie2.Expires = DateTime.Now.AddYears(-1)
        Response.Cookies.Add(cookie2)
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
        HttpContext.Current.Request.Cookies.Clear()
        HttpContext.Current.Response.Cookies.Clear()
        HttpContext.Current.Application.Clear()
        HttpContext.Current.Session.RemoveAll()


        Dim nome As String = HttpContext.Current.User.Identity.Name
        Response.Redirect(ResolveUrl("~/Login.aspx"))


    End Sub
#End Region



    'Protected Sub LoginStatus1_LoggingOut(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LoginCancelEventArgs) Handles LoginStatus1.LoggingOut
    '    Dim userName As String = CType(Session("userName"), String)

    '    Dim ASPNETContext = CType(Application("sessionSortedList"), SortedDictionary(Of String, SessionContext))
    '    Dim currentSessionContext As SessionContext = Nothing
    '    If Not ASPNETContext Is Nothing Then
    '        ASPNETContext.TryGetValue(userName, currentSessionContext)
    '        ' Refresh the object to the Application 
    '        If Not currentSessionContext Is Nothing Then
    '            ASPNETContext.Remove(userName)
    '            Application("sessionSortedList") = ASPNETContext
    '        End If
    '    End If
    '    FormsAuthentication.SignOut()
    '    Session.Clear()
    '    Session.Abandon()
    '    HttpContext.Current.Response.Cookies("ASP.NET_SessionId").Value = ""
    'End Sub
End Class

